﻿Imports System.Data.OleDb

Public Class UserProfile
    Inherits System.Web.UI.Page

    

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            Dim sql As String = "SELECT * FROM UserProfile WHERE Username= @f1"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            conn.Open()
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)

            'Creating a data reader to read data
            Dim datareader = cmd.ExecuteReader()
            If datareader.HasRows Then
                datareader.Read() 
                If Not IsDBNull(datareader("AboutMe")) Then txtAboutMe.Text = datareader("AboutMe")
                If Not IsDBNull(datareader("UserImage")) Then userImage.ImageUrl = datareader("UserImage")
            Else
                userImage.ImageUrl = "/Images/User.png"
            End If
            datareader.Close()
            cmd.Dispose()
            conn.Close()
            conn.Dispose()
        End If
    End Sub

    Protected Sub txtSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles txtSubmit.Click
        Dim newFileLoc As String = ""
        Dim sql As String
     
        Dim myGUID = Guid.NewGuid()
        Dim newFileName As String = myGUID.ToString() & ".jpg"
        Dim fileLocationOnDisk = Request.MapPath("Images/UserImages") & "/" & newFileName
        txtImage.SaveAs(fileLocationOnDisk)
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If txtImage.HasFile Then
            sql = "UPDATE UserProfile SET UserImage=@f1, AboutMe=@f2 WHERE Username=@f3"
        Else
            sql = "UPDATE UserProfile SET AboutMe=@f2 WHERE Username=@f3"
        End If
        Dim cmd As New OleDbCommand(sql, conn)
        conn.Open()

        If txtImage.HasFile Then cmd.Parameters.AddWithValue("@f1", "Images/UserImages/" & newFileName)
        cmd.Parameters.AddWithValue("@f2", txtAboutMe.Text)
        cmd.Parameters.AddWithValue("@f3", User.Identity.Name)

        cmd.ExecuteNonQuery()

        cmd.Dispose()
        conn.Close()
        conn.Dispose()
        Response.Redirect("UserProfile.aspx?Username=" & User.Identity.Name)

    End Sub


End Class